﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HRM.Model;
using System.Collections.Generic;
using HRM.Helper;
using System.Windows;

namespace HRM.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class DepartmentViewModel : ViewModelGeneric<PhongBan>
    {
        private IDepartmentService _departmentService;

        #region Search Property

        private int _searchMode = 2;

        public int SearchMode
        {
            get { return _searchMode; }
            set
            {
                if (_searchMode != value)
                {
                    _searchMode = value;
                    RaisePropertyChanged("SearchMode");
                }
            }
        }

        private string _searchInput = string.Empty;

        public string SearchInput
        {
            get { return _searchInput; }
            set
            {
                if (_searchInput != value)
                {
                    _searchInput = value;
                    RaisePropertyChanged("SearchInput");
                }
            }
        }

        #endregion

        #region Commands

        public RelayCommand SearchCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (SearchInput != "")
                    {
                        var list = _departmentService.SearchBy(SearchMode, SearchInput);
                        if (list.Count == 0) Messenger.Default.Send(new StatusMessage("Không tìm thấy", 3000, StatusType.Warning));
                        else
                            Messenger.Default.Send(new StatusMessage(string.Format("Tìm thấy {0} bản ghi", list.Count), 3000, StatusType.Done));
                        ListEntities = list;
                    }
                    else Messenger.Default.Send(StatusMessage.EmptyInputMessage());
                });
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the DepartmentViewModel class.
        /// </summary>
        public DepartmentViewModel(IDepartmentService service, IEntityService<PhongBan> entityService) : base(entityService)
        {
            _departmentService = service;
        }
    }
}